﻿using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using static IronPython.Modules._ast;

namespace CoreLims.Models.CoreLims
{
    public static class WikiExtension
    {
        /// <summary>
        /// 返回文档库列表
        /// </summary>
        /// <param name="Library"></param>
        /// <param name="dbContext"></param>
        /// <returns></returns>
        public static List<WikiLibrary> getLibrarys(this WikiLibrary Library, CoreLimsContext dbContext) =>
            dbContext.Library.Where(s => s.ParentDirectory == null).OrderBy(x => x.Name).ToList();

        /// <summary>
        /// 获得指定目录下的所有目录信息
        /// </summary>
        /// <param name="Library"></param>
        /// <param name="dbContext"></param>
        /// <param name="Diretory"></param>
        /// <returns></returns>
        public static List<WikiLibrary> getDirectorys(this WikiLibrary Library, CoreLimsContext dbContext, string Directory) =>
            dbContext.Library.Where(s => s.ParentDirectory == Directory).OrderBy(x => x.Name).ToList();

        /// <summary>
        /// 获取指定文章内容
        /// </summary>
        /// <param name="page"></param>
        /// <param name="dbContext"></param>
        /// <param name="PageId"></param>
        /// <returns></returns>
        public static WikiPage getPage(this WikiPage page, CoreLimsContext dbContext, string PageId) =>
            dbContext.Page.FirstOrDefault(s => s.Id == PageId);

        /// <summary>
        /// 获取文章关联的评论
        /// </summary>
        /// <param name="PageComment"></param>
        /// <param name="dbContext"></param>
        /// <param name="PageId"></param>
        /// <returns></returns>
        public static List<WikiPageComment> getPageComment(this WikiPageComment PageComment, CoreLimsContext dbContext, string PageId) =>
            dbContext.PageComment.Where(s => s.PageId == PageId).OrderBy(s => s.RecordTime).ToList();

        /// <summary>
        /// 获取指定目录下的文章列表
        /// </summary>
        /// <param name="Library"></param>
        /// <param name="dbContext"></param>
        /// <param name="Directory"></param>
        /// <returns></returns>
        public static List<WikiPage> getPages(this WikiPage page, CoreLimsContext dbContext, string Directory) =>
            dbContext.Page.Where(s => s.ParentDirectory == Directory).OrderBy(x => x.Title).ToList();

        /// <summary>
        /// 创建文档库
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="Name">文档库名称</param>
        /// <param name="Description">文档库描述</param>
        /// <param name="CreateUserId">创建者ID</param>
        /// <param name="CreateUserName">创建者姓名</param>
        /// <param name="ParentDirectory">如果创建的是根级目录，内容应该为 NULL </param>
        /// <returns></returns>
        public static string CreateLibrary(this WikiLibrary Library, CoreLimsContext dbContext, string Name, string CreateUserId, string CreateUserName, string ParentDirectory = null)
        {
            if (string.IsNullOrEmpty(Name))
                return "文档库名称不能为空！";

            var exits = dbContext.Library.Where(s => s.Name == Name).Count();
            if (exits > 0)
                return "文档库名称不能重复！";

            var lib = new WikiLibrary();
            lib.Name = Name;
            lib.CreateUserId = CreateUserId;
            lib.CreateUserName = CreateUserName;
            lib.ParentDirectory = ParentDirectory;

            dbContext.Library.Add(lib);
            dbContext.SaveChanges();

            return "文档库添加成功！";
        }

        /// <summary>
        /// 创建文档库文章
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="dbContext"></param>
        /// <param name="Title"></param>
        /// <param name="CreatorId"></param>
        /// <param name="CreatorName"></param>
        /// <param name="ParentDirectory"></param>
        /// <returns></returns>
        public static string CreatePage(this WikiPage Page, CoreLimsContext dbContext, string Title, string CreatorId, string CreatorName, string ParentDirectory)
        {
            if (string.IsNullOrEmpty(Title))
                return "文章名不能为空！";

            var page = new WikiPage();
            page.Title = Title;
            page.CreatorId = CreatorId;
            page.CreatorName = CreatorName;
            page.ParentDirectory = ParentDirectory;

            dbContext.Page.Add(page);
            dbContext.SaveChanges();

            return "文章添加成功！";
        }

        /// <summary>
        /// 根据ID查询当前文档库的父目录位置
        /// </summary>
        /// <param name="Library"></param>
        /// <param name="dbContext"></param>
        /// <param name="WikiId"></param>
        /// <returns></returns>
        public static string SearchDirectory(this WikiLibrary Library, CoreLimsContext dbContext, string WikiId)
        {
            var lib = dbContext.Library.FirstOrDefault(s => s.Id == WikiId);
            if (lib != null)
            {
                if (lib.ParentDirectory.IsNullOrEmpty())
                    return "\\" + lib.Name;
                else
                    return lib.ParentDirectory + "\\" + lib.Name;
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 添加目录
        /// </summary>
        /// <param name="Library"></param>
        /// <param name="dbContext"></param>
        /// <param name="ParentDirectory">当前目录所属父目录</param>
        /// <param name="DirectoryName">目录名称</param>
        /// <param name="CreateUserId">创建者ID</param>
        /// <param name="CreateUserName">创建者姓名</param>
        /// <returns></returns>
        public static string AddDirectory(this WikiLibrary Library, CoreLimsContext dbContext, string ParentDirectory, string DirectoryName, string CreateUserId, string CreateUserName)
        {
            var wiki = new WikiLibrary();
            wiki.Name = DirectoryName;
            wiki.ParentDirectory = ParentDirectory;
            wiki.CreateUserId = CreateUserId;
            wiki.CreateUserName = CreateUserName;

            dbContext.Library.Add(wiki);
            dbContext.SaveChanges();
            return "目录添加成功！";
        }

        // 以下是文件管理的内容

        /// <summary>
        /// 获得顶级目录
        /// </summary>
        /// <param name="file"></param>
        /// <param name="dbContext"></param>
        /// <returns></returns>
        public static List<WikiFileManager> getTopLevel(this WikiFileManager files, CoreLimsContext dbContext) =>
            dbContext.WikiFileManager.Where(s => s.ParentDirectory == null).OrderBy(x => x.FileName).ToList();

        public static WikiFileManager getFileManager(this WikiFileManager files, CoreLimsContext dbContext, string Id) =>
            dbContext.WikiFileManager.FirstOrDefault(s => s.Id == Id);

        /// <summary>
        /// 根据目录ID，获取该目录下的所有文件
        /// </summary>
        /// <param name="files"></param>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static List<WikiFileManager> getWikiFiles(this WikiFileManager files, CoreLimsContext dbContext, string Id) =>
            dbContext.WikiFileManager.Where(s => s.ParentDirectory == Id && s.IsDirectory == false)
            .OrderBy(s => s.FileName).OrderBy(s => s.Version).ToList();

        /// <summary>
        /// 获得指定目录ID的下级目录
        /// </summary>
        /// <param name="files"></param>
        /// <param name="dbContext"></param>
        /// <param name="DirectoryId"></param>
        /// <returns></returns>
        public static List<WikiFileManager> getDirectory(this WikiFileManager files, CoreLimsContext dbContext, string DirectoryId) =>
            dbContext.WikiFileManager.Where(s => s.ParentDirectory == DirectoryId && s.IsDirectory == true).OrderBy(x => x.FileName).ToList();

        /// <summary>
        /// 创建文件分类
        /// </summary>
        /// <param name="file"></param>
        /// <param name="dbContext"></param>
        /// <param name="Name"></param>
        /// <param name="CreateUserId"></param>
        /// <param name="CreateUserName"></param>
        /// <param name="ParentDirectory"></param>
        /// <returns></returns>
        public static string CreateDirectory(this WikiFileManager files, CoreLimsContext dbContext, string Name, string CreateUserId, string CreateUserName, string ParentDirectory = null)
        {
            if (string.IsNullOrEmpty(Name))
                return "分类名不能为空！";
            
            var exits = dbContext.WikiFileManager.Where(s => s.FileName == Name && s.ParentDirectory == ParentDirectory).Count();
            if (exits > 0)
                return "同级目录中分类名不能重复！";

            var file = new WikiFileManager();
            file.FileName = Name;
            file.CreatorId = CreateUserId;
            file.CreatorName = CreateUserName;
            file.IsDirectory = true;
            file.ParentDirectory = ParentDirectory;

            dbContext.WikiFileManager.Add(file);
            dbContext.SaveChanges();

            return "文档分类添加成功！";
        }

        /// <summary>
        /// 根据ID返回当前记录所在位置字符串
        /// </summary>
        /// <param name="files"></param>
        /// <param name="dbContext"></param>
        /// <param name="FileId"></param>
        /// <returns></returns>
        public static string SearchFileDirectory(this WikiFileManager files, CoreLimsContext dbContext, string FileId)
        {
            var file = dbContext.WikiFileManager.FirstOrDefault(s => s.Id == FileId);
            StringBuilder st = new StringBuilder();

            while (file != null)
            {
                st = st.Insert(0, "\\" + file.FileName);
                file = dbContext.WikiFileManager.FirstOrDefault(s => s.Id == file.ParentDirectory);
            }
            return st.ToString();
        }

    }
}
